<?php


namespace app\index\controller;
use app\BaseController;

//引入模型
use app\model\Test;

//本控制器用于测试操作数据库-模型
class Model extends BaseController
{

    public function index()
    {
        //模型使用案例
        //Test::where('id','<',10)->select();

        // suffix方法用于静态查询，设置表后缀
        //$test = Test::suffix('_en')->find();
        //$test->name = 'test';
        //$test->save();

        //########## 新增
        //$user           = new User;
        //$user->name     = 'thinkphp';
        //$user->email    = 'thinkphp@qq.com';
        //$user->save();
        //$user->replace()->save();

        //$user = new User;
        //$user->save([
        //    'name'  =>  'thinkphp',
        //    'email' =>  'thinkphp@qq.com'
        //]);

        //$user = new User;
        // 过滤post数组中的非数据表字段数据
        //$data = Request::only(['name','email']);
        //$user->save($data);

        //批量新增
        //$user = new User;
        //$list = [
        //    ['name'=>'thinkphp','email'=>'thinkphp@qq.com'],
        //    ['name'=>'onethink','email'=>'onethink@qq.com']
        //];
        //$user->saveAll($list);

        //静态方法新增
        //$user = User::create([
        //    'name'  =>  'thinkphp',
        //    'email' =>  'thinkphp@qq.com'
        //]);
        //echo $user->name;
        //echo $user->email;
        //echo $user->id; // 获取自增ID

        //$user = User::create([
        //    'name'  =>  'thinkphp',
        //    'email' =>  'thinkphp@qq.com'
        //], ['name','email'], true); //参数二是允许写入的字段，参数三是replace操作


        //########## 更新
        //最佳更新方式
        //$user = User::where('status',1)
        //	->where('name','liuchen')
        //	->find();
        //$user->name     = 'thinkphp';
        //$user->email    = 'thinkphp@qq.com';
        //$user->score	=  Db::raw('score+1'); //原生语句
        //$user->save();

        //批量更新
        //$user = new User;
        //$list = [
        //    ['id'=>1, 'name'=>'thinkphp', 'email'=>'thinkphp@qq.com'],
        //    ['id'=>2, 'name'=>'onethink', 'email'=>'onethink@qq.com']
        //];
        //$user->saveAll($list);

        //静态方法更新
        //User::update(['name' => 'thinkphp','email' => 'thinkphp@qq.com'], ['id' => 1]);

        //########## 删除
        //查询后调用delete方法
        //$user = User::find(1);
        //$user->delete();

        //主键删除
        //User::destroy(1);
        //// 支持批量删除多个数据
        //User::destroy([1,2,3]);

        //User::where('id','>',10)->delete();

        //########## 查询
        //$user = User::find($id);
        //$user = User::where('name', 'thinkphp')->find();

        //$user = User::findOrEmpty(1);
        //if ($user->isEmpty()) {
        //    echo '空模型';
        //}

        //判断是否为空不能直接用empty(),而要用数据集对象的isEmpty方法
        //$users = User::select();
        //if($users->isEmpty()){
        //    echo '数据集为空';
        //}

        //// 根据主键获取多个数据
        //$list = User::select([1,2,3]);
        //// 对数据集进行遍历操作
        //foreach($list as $key=>$user){
        //    echo $user->name;
        //}

        //// 使用查询构造器查询
        //$list = User::where('status', 1)->limit(3)->order('id', 'asc')->select();
        //foreach($list as $key=>$user){
        //    echo $user->name;
        //}
        //其他更多方法同链式查询

        //##### 闭包查询
        //User::scope(function($query){
        //    $query->where('age','>',20)->limit(10);
        //})->select();




    }



}